<?php

namespace app\api\controller;

use app\common\controller\ApiAdminBase;
use app\common\helpers\Functions;
use think\Request;

class VisitRecord extends ApiAdminBase
{
    use \app\api\traits\VisitRecord;

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $input = $request->param();
        $per_page = $input['per_page']??10;
        $condition = [];
        $data = \app\common\model\VisitRecord::getList($condition,[],['id desc'],$per_page);
        $data = $data ? $data->toArray() : [];
        $data['data'] = isset($data['data']) ? self::formatList($data['data']) : [];
        return $this->reply($data);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function add()
    {
        $ip = Functions::getIp();
        $visitRecord = new \app\common\model\VisitRecord();
        $visitRecord->ip = $ip;

        $remark = '';
        $agentInfo = $_SERVER['HTTP_USER_AGENT'];

        $mobileInfo = Functions::getClientMobileBrand($agentInfo);
        $browserInfo = Functions::getClientBrowser($agentInfo);
        $osInfo = Functions::getClientOS($agentInfo);

        $remark .= '手机：'.$mobileInfo['mobile_brand'].'，'.$mobileInfo['mobile_ver'].'；<br />';
        $remark .= '浏览器：'.$browserInfo['browser'].'，'.$browserInfo['browser_ver'].'；<br />';
        $remark .= '操作系统：'.$osInfo['os'].'，'.$osInfo['os_ver'].'，'.$osInfo['equipment'].'；<br />';

        $visitRecord->module = $_SERVER['HTTP_HOST'].',index';
        $visitRecord->region = '--';
        $visitRecord->remark = $remark;
        $visitRecord->agent_info = $agentInfo;

        $visitRecord->create_time = time();
        $visitRecord->update_time = $visitRecord->create_time;
        $visitRecord->create_date = date('Y-m-d H:i:s',$visitRecord->create_time);
        $visitRecord->save();
        return $this->reply(['id'=>$visitRecord->id]);
    }

}
